<?php
class stFacebookDashboard_model extends stmodel_2_1{
	public $fan_count,$date_week,$like_last_week,$last_week,$fan_count_last_week,$page_url,$likes,$fan_count_yesterday,$yesterday,$most_like,$likes_yesterday,$form_submit,$form_today,$form_yesterday,$total_card,$total_share_fb,$total_user,$total_share_email;
    public function __construct($config) {
        parent::__construct ( $config );
        require_once $this->config['pluginDir'] . 'functions.php';
        $fucntionName = $config['plugin_name'] . "Functions";
        $this->functions = new $fucntionName($this->config);
    }
    public function return_dashboard(){
    	global $wpdb,$current_user;
    	$time = mktime();
		$last = intval($time) - 24*3600;
		$yesterday =  date('d-m-Y', $last);
		$this->yesterday = $yesterday;
        $today = date('d-m-Y');
        $date_wee = $time - (date("w")*3600*24)+3600*24;
        $date_week = date('d-m-Y',$date_wee);
        $this->date_week = $date_week;
        $last_week = $time - 3600*24*7;
        $this->last_week = date('d-m-Y',$last_week);
        $fan_count_last_week = $wpdb->get_var("SELECT SUM(fancount) FROM st_likepluginfbpagestatistics WHERE date='".$this->last_week."' AND user_id = $current_user->ID");
        if(!$fan_count_last_week)
        {
            $fan_count_last_week = 0;
        }
        $fan_count_last_date_week = $wpdb->get_var("SELECT SUM(fancount) FROM st_likepluginfbpagestatistics WHERE date = '".date('d-m-Y',($date_wee-7*24*3600))."'  AND user_id = $current_user->ID");
        $this->fan_count_last_week = $fan_count_last_week - $fan_count_last_date_week;
        $fan_count_yesterday = $wpdb->get_var("SELECT SUM(fancount) FROM st_likepluginfbpagestatistics WHERE date='".$date_week."' AND user_id = $current_user->ID");
        if(!$fan_count_yesterday)
        {
            $fan_count_yesterday = 0;
        }
        $this->date_week = $date_week;
        $fan_count_today = $wpdb->get_var("SELECT SUM(fancount) FROM st_likepluginfbpagestatistics WHERE user_id = $current_user->ID AND date='".date('d-m-Y',($time-24*3600))."'");
        if(!$fan_count_today)
        {
            $fan_count_today = 0;
        }
        
        $this->fan_count = intval($fan_count_today) - intval($fan_count_yesterday);
        $check_fan = $this->fan_count - $this->fan_count_last_week;
        $total_fan = $wpdb->get_var("SELECT SUM(fancount) FROM st_likepluginfbpagestatistics WHERE user_id = $current_user->ID");
        $this->fan_count_last_week = @round(($check_fan/$total_fan)*100,2);
        $most_like = $wpdb->get_row("SELECT * FROM `st_likepluginfbpagestatistics` WHERE user_id = $current_user->ID ORDER BY numberoflikes DESC LIMIT 0,1");
        $this->most_like = $most_like->numberoflikes;
        $this->page_url = $wpdb->get_var("SELECT pageurl FROM st_likepluginfbpagelist WHERE pageid=".$most_like->facebookpageid);
        $this->fan_count_yesterday = $fan_count_yesterday;
        //--- total likes
        $likes_yesterday = $wpdb->get_var("SELECT SUM(`numberofposts`) + SUM(`numberofcomments`)+SUM(`numberoflikes`)+SUM(`numberofshares`)+SUM(`numberofusers`) FROM st_likepluginfbpagestatistics WHERE user_id = $current_user->ID AND  date='".$date_week."'");
        if(!$likes_yesterday)
        {
            $likes_yesterday = 0;
        }
        $likes_results = $wpdb->get_results("SELECT * FROM (SELECT * FROM `st_likepluginstatistics` WHERE user_id=$current_user->ID ORDER BY `id` DESC) l WHERE user_id=$current_user->ID GROUP BY url ORDER BY url DESC ");
        $likes_today = 0;
        foreach($likes_results as $likes)
        {
            $likes_today +=$likes->total_like;
        }
        if(!$likes_today)
        {
            $likes_today = 0;
        }
        $likes_results = $wpdb->get_results("SELECT * FROM (SELECT * FROM `st_likepluginstatistics` WHERE user_id=$current_user->ID AND date = '$this->last_week' ORDER BY `id` DESC) l WHERE user_id=$current_user->ID  AND date = '$this->last_week' GROUP BY url ORDER BY url DESC ");
        $like_last_week = 0;
        foreach($likes_results as $likes){
            $like_last_week += $likes->total_like;
        }
        $likes_results = $wpdb->get_results("SELECT * FROM (SELECT * FROM `st_likepluginstatistics` WHERE user_id=$current_user->ID AND date = '".date('d-m-Y',($date_wee-7*24*3600))."' ORDER BY `id` DESC) l WHERE user_id=$current_user->ID  AND date = '".date('d-m-Y',($date_wee-7*24*3600))."' GROUP BY url ORDER BY url DESC ");
        $like_last_week_end = 0;
         foreach($likes_results as $likes){
            $like_last_week_end += $likes->total_like;
        }
        $likes_results = $wpdb->get_results("SELECT * FROM (SELECT * FROM `st_likepluginstatistics` WHERE user_id=$current_user->ID AND date = '".date('d-m-Y',($date_wee))."' ORDER BY `id` DESC) l WHERE user_id=$current_user->ID  AND date = '".date('d-m-Y',($date_wee))."' GROUP BY url ORDER BY url DESC ");
        $like_week_end = 0;
        foreach($likes_results as $likes){
            $like_week_end += $likes->total_like;
        }
        $like_results = $wpdb->get_results("SELECT * FROM st_likepluginstatistics WHERE user_id=$current_user->ID");
        $totals_like = 0;
        foreach($like_results as $likes){
            $total_likes += $likes->total_like;
        }
        $this->like_last_week = $like_last_week - $like_last_week_end;
        $like_week_end = $likes_today - $like_week_end;
        $this->likes = $likes_today;
        //$this->likes = $total_likes;
        $this->like_last_week = $like_week_end - $this->like_last_week;
        if($total_likes > 0){
        $this->like_last_week = round(($this->like_last_week/$total_likes)*100,2);
        }else{
            $this->like_last_week = 0;
        }
        //$this->likes = $like_week_end;
        $this->likes_yesterday = $likes_yesterday;
        $this->form_submit = $wpdb->get_var("SELECT COUNT(*) FROM st_ap_form_value WHERE form_id IN (SELECT form_id FROM st_ap_forms WHERE user_id = {$current_user->ID})");
        $form_today = date('Y-m-d');
        $this->form_today = $wpdb->get_var("SELECT COUNT(*) FROM st_ap_form_value WHERE form_id IN (SELECT form_id FROM st_ap_forms WHERE user_id = {$current_user->ID}) AND DATE(`date_created`) = '{$form_today}'");
        $form_yesterday =  date('Y-m-d', $last);
        $this->form_yesterday = $wpdb->get_var("SELECT COUNT(*) FROM st_ap_form_value WHERE form_id IN (SELECT form_id FROM st_ap_forms WHERE user_id = {$current_user->ID}) AND DATE(`date_created`) = '{$form_yesterday}'");
        $smt_user_id = $this->functions->getCurrentSmtUserId($this->config);
        $current_bucket = $this->config['folder_client'];
        $current_bucket_id = $wpdb->get_var("SELECT id FROM wp_st_postcard_bucket WHERE name like ('$current_bucket') ");
        $this->total_card = $wpdb->get_var("SELECT count(id) FROM wp_st_postcard_card WHERE smt_user_id = $smt_user_id  AND `bucket_id` = $current_bucket_id");
        $this->total_share_fb = $wpdb->get_var("SELECT sum(table1.shared_email) FROM wp_st_postcard_card card LEFT JOIN (SELECT card.id id , sum(share.number_share) shared_email FROM wp_st_postcard_card card  LEFT JOIN wp_st_postcard_share share on share.card_id = card.id  WHERE `type_share` like('facebook') GROUP BY card.id) table1 ON table1.id = card.id  WHERE smt_user_id = $smt_user_id  AND `bucket_id` = $current_bucket_id GROUP BY card.smt_user_id");
        $this->total_user = $wpdb->get_var("SELECT count(id) FROM wp_st_postcard_user WHERE smt_user_id = $smt_user_id");
        $this->total_share_email = $wpdb->get_var("SELECT sum(table1.shared_email) FROM wp_st_postcard_card card LEFT JOIN (SELECT card.id id , sum(share.number_share) shared_email FROM wp_st_postcard_card card  LEFT JOIN wp_st_postcard_share share on share.card_id = card.id  WHERE `type_share` like('email') GROUP BY card.id) table1 ON table1.id = card.id  WHERE smt_user_id = $smt_user_id  AND `bucket_id` = $current_bucket_id GROUP BY card.smt_user_id");
        
    }
    
    public function echo_dashboard($id)
    {
  		if($id==1){
  			?>
    		 <li>
                          <div class="dashboard_border" id="fan_growth">
	                          <div class="dashboard_content">Fan Growth This week</div>
	                          <div class="dashboard_number"><?php if($this->fan_count > 0 ) {echo '+';} echo $this->fan_count;?></div>
	                          <div class="dashboard_total">Total new fans</div>
	                          <div class="dashboard_percent"><?php  
                                  echo $this->fan_count_last_week;?>%</div>
	                          <div class="dashboard_detail">compared to <?php echo str_replace('-','/',$this->last_week);?> - <?php echo date('d/m/Y');?></div>
	                          <div class="dashboard_button"><a class="button add-new-h2" onclick="stFacebookToolbox.loadPage('page_analytics',null,null,function(){stFacebookToolbox.loadPage('fan_count');});" href="javascript:void(0);">View Report</a></div>
						  </div>
                    </li>
		
  		<?php }
  		elseif($id==2){?>
  		 <li>
                          <div class="dashboard_border" id="most_like_link">
	                          <div class="dashboard_content">Most Liked Link</div>
	                          <div class="dashboard_number"><?php echo $this->most_like;?></div>
	                          <div class="dashboard_total">Total likes</div>
	                          <div class="dashboard_detail"><?php echo $this->page_url;?></div>
	                          <div class="dashboard_button"><a class="button add-new-h2" onclick="window.open('<?php echo $this->page_url;?>');" href="javascript:void(0);">Visit link</a></div>
						  </div>
                    </li>
  		<?php }
  		elseif($id==3) {?>
  		<li>
                          <div class="dashboard_border" id="website_viral_spread">
	                         <div class="dashboard_content">Website Viral Spread</div>
	                         <div class="dashboard_number"><?php echo $this->likes;?></div>
	                         <div class="dashboard_total">Totals Likes</div>
	                         <div class="dashboard_percent"><?php echo $this->like_last_week?>%</div>
	                         <div class="dashboard_detail">compared to <?php echo str_replace('-','/',$this->last_week);?> - <?php echo date('d/m/Y');?></div>
	                         <div class="dashboard_button"><a class="button add-new-h2" onclick="stFacebookToolboxcustom.loadPage('like_analytics');" href="javascript:void(0);">View Report</a></div>
						  </div>
                    </li>
  		<?php }	
                elseif($id==4) {
                    ?>
                    <li>
                          <div class="dashboard_border" id="form_builder_stats">
	                         <div class="dashboard_content">Form builder stats</div>
	                         <div class="dashboard_number"><?php echo $this->form_submit;?></div>
	                         <div class="dashboard_total">Total form submissions</div>
	                         <div class="dashboard_detail" style="margin-bottom:2px;">Form submissions today:<?php echo $this->form_today;?></div>
                                 <div class="dashboard_detail" style="margin-bottom:15px;">Form submissions yesterday:<?php echo $this->form_yesterday;?></div>
	                         <div class="dashboard_button"><a class="button add-new-h2" onclick="window.location='<?php echo get_bloginfo('url');?>/wp-admin/admin.php?page=stFacebookToolbox&action=applications&redirect=1';" href="javascript:void(0);">Go to application</a></div>
						  </div>
                    </li>
                    <?php 
                }
                else { ?>
                    <li>
                          <div class="dashboard_border" id="postcard_stats">
	                         <div class="dashboard_content">Postcard stats</div>
	                         <div class="dashboard_total" style="font-weight: lighter;">No. of card created:<b><?php echo $this->total_card;?></b></div>
                                 <div class="dashboard_total"  style="font-weight: lighter;">No. of shares on facebook:<b><?php echo intval($this->total_share_fb);?></b></div>
                                 <div class="dashboard_total"  style="font-weight: lighter;">No. of users:<b><?php echo intval($this->total_user);?></b></div>
                                 <div class="dashboard_total"  style="font-weight: lighter;"> No. of shares on email:<b><?php echo intval($this->total_share_email);?></b></div>
	                         <div class="dashboard_button"><a class="button add-new-h2" onclick="window.location='<?php echo get_bloginfo('url');?>/wp-admin/admin.php?page=stFacebookToolbox&action=dashboardPostcard&redirect=1';" href="javascript:void(0);">Go to application</a></div>
						  </div>
                    </li>
                    <?php 
                }
    }

}
?>